package com.shujia.wyh.day19;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
    List集合存储自定义对象并使用get()和size()方法结合遍历
 */
public class ListDemo2 {
    public static void main(String[] args) {
        //创建迭代器对象
        List list = new ArrayList();

        //创建3个学生对象
        Student s1 = new Student("小王", 18);
        Student s2 = new Student("小李", 17);
        Student s3 = new Student("小张", 16);

        //向集合中添加元素
        list.add(s1);
        list.add(s2);
        list.add(s3);

//        for(...){
//            //创建对象
//            //add()
//        }

        //遍历集合
        //方式1：迭代器遍历
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            Student student = (Student) next;
            System.out.println(student.getName() + "---" + student.getAge());
        }
        System.out.println("=============================");
        //方式2：
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            Student s = (Student) o;
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
